#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->aiplayer_1->number=1;
    ui->aiplayer_2->number=2;
    ui->aiplayer_3->number=3;
    ui->aiplayer_4->number=4;
    ui->aiplayer_5->number=5;
    connect(ui->startgame,SIGNAL(released()),this,SLOT(Starter()));
    connect(ui->mylook,SIGNAL(released()),this,SLOT(mylooked()));
    connect(ui->mygiveup,SIGNAL(released()),this,SLOT(mygived()));
    connect(ui->myonce,SIGNAL(released()),this,SLOT(myonced()));
    connect(ui->mypass,SIGNAL(released()),this,SLOT(mypassed()));
    connect(ui->mytwice,SIGNAL(released()),this,SLOT(mytwiced()));
    connect(ui->mytreble,SIGNAL(released()),this,SLOT(mytrebled()));
    connect(ui->battleyou_1,SIGNAL(released()),this,SLOT(beginbattle1()));
    connect(ui->battleyou_2,SIGNAL(released()),this,SLOT(beginbattle2()));
    connect(ui->battleyou_3,SIGNAL(released()),this,SLOT(beginbattle3()));
    connect(ui->battleyou_4,SIGNAL(released()),this,SLOT(beginbattle4()));
    connect(ui->battleyou_5,SIGNAL(released()),this,SLOT(beginbattle5()));
}
void Widget::gameloop()
{
    qDebug()<<"gameloop";
    N = new Handout;
    N->Updatepocker();
    onescard[0]=N->dis1(0);
    onescard[1]=N->dis2(0);
    onescard[2]=N->dis3(0);
    ui->aiplayer_1->onescard[0]=N->dis1(1);
    ui->aiplayer_1->onescard[1]=N->dis2(1);
    ui->aiplayer_1->onescard[2]=N->dis3(1);
    ui->aiplayer_2->onescard[0]=N->dis1(2);
    ui->aiplayer_2->onescard[1]=N->dis2(2);
    ui->aiplayer_2->onescard[2]=N->dis3(2);
    ui->aiplayer_3->onescard[0]=N->dis1(3);
    ui->aiplayer_3->onescard[1]=N->dis2(3);
    ui->aiplayer_3->onescard[2]=N->dis3(3);
    ui->aiplayer_4->onescard[0]=N->dis1(4);
    ui->aiplayer_4->onescard[1]=N->dis2(4);
    ui->aiplayer_4->onescard[2]=N->dis3(4);
    ui->aiplayer_5->onescard[0]=N->dis1(5);
    ui->aiplayer_5->onescard[1]=N->dis2(5);
    ui->aiplayer_5->onescard[2]=N->dis3(5);
    ui->aiplayer_1->judge();
    ui->aiplayer_2->judge();
    ui->aiplayer_3->judge();
    ui->aiplayer_4->judge();
    ui->aiplayer_5->judge();
    myjudge();
   /* qDebug()<<ui->aiplayer_1->type;
    qDebug()<<ui->aiplayer_1->capital[0]<<ui->aiplayer_1->capital[1];
    qDebug()<<ui->aiplayer_1->sub<<ui->aiplayer_1->subsub;
    qDebug()<<ui->aiplayer_1->onescard[0]<<ui->aiplayer_1->onescard[1]<<ui->aiplayer_1->onescard[2];*/
    myturn();
}

void Widget::Starter()
{
    ui->startgame->setDisabled(true);
    qDebug()<<"Starter";
    gameloop();
}

void Widget::myturn()
{
    qDebug()<<"myturn";
    if(!ismylooked)
    {
    ui->mylook->setEnabled(true);
    }
    if(mycoin>=current&&!ismylooked)
    {
    ui->myonce->setEnabled(true);
    }
    if(mycoin>=current*2)
    {
    ui->mytwice->setEnabled(true);
    }
    if(mycoin>=current*4)
    {
    ui->mytreble->setEnabled(true);
    }
    if(ismylooked&&current*2>mycoin)
    {
     ui->myshowhand->setEnabled(true);
    }
}
void Widget::mylooked()
{
     ismylooked=true;
     ui->myonce->setDisabled(true);
     ui->mylook->setDisabled(true);
     ui->mygiveup->setEnabled(true);
     update();
}
void Widget::mypassed()
{
    if(ui->aiplayer_1->alive)
    {
        ui->battleyou_1->setDisabled(true);
    }
    if(ui->aiplayer_2->alive)
    {
        ui->battleyou_2->setDisabled(true);
    }
    if(ui->aiplayer_3->alive)
    {
        ui->battleyou_3->setDisabled(true);
    }
    if(ui->aiplayer_4->alive)
    {
        ui->battleyou_2->setDisabled(true);
    }
    if(ui->aiplayer_5->alive)
    {
        ui->battleyou_5->setDisabled(true);
    }
    ui->mypass->setDisabled(true);
    turn1();
}
void Widget::myonced()
{
    mycoin-=current;
    total+=current;
    ui->total_show->setText(QString::number(total));
    ui->mygiveup->setDisabled(true);
    ui->mylook->setDisabled(true);
    ui->myonce->setDisabled(true);
    ui->mytreble->setDisabled(true);
    ui->mytwice->setDisabled(true);
    ui->my_show->setText(QString::number(mycoin));
}
void Widget::mytwiced()
{
    mycoin-=2*current;
    total+=2*current;
    if(!ismylooked)
    {
        current*=2;
        ui->current_show->setText(QString::number(current));
        turn1();
    }
    else
    {
        if(ui->aiplayer_1->alive)
        {
            ui->battleyou_1->setEnabled(true);
        }
        if(ui->aiplayer_2->alive)
        {
            ui->battleyou_2->setEnabled(true);
        }
        if(ui->aiplayer_3->alive)
        {
            ui->battleyou_3->setEnabled(true);
        }
        if(ui->aiplayer_4->alive)
        {
            ui->battleyou_4->setEnabled(true);
        }
        if(ui->aiplayer_5->alive)
        {
            ui->battleyou_5->setEnabled(true);
        }
        ui->mypass->setEnabled(true);
    }
    ui->mygiveup->setDisabled(true);
    ui->mylook->setDisabled(true);
    ui->myonce->setDisabled(true);
    ui->mytreble->setDisabled(true);
    ui->mytwice->setDisabled(true);
    ui->my_show->setText(QString::number(mycoin));
}
void Widget::mytrebled()
{
    mycoin-=4*current;
    total+=4*current;
    ui->total_show->setText(QString::number(total));
    if(!ismylooked)
    {
        current*=4;
        ui->current_show->setText(QString::number(current));
        turn1();
    }
    else
    {
        current*=2;
        ui->current_show->setText(QString::number(current));
        if(ui->aiplayer_1->alive)
        {
            ui->battleyou_1->setEnabled(true);
        }
        if(ui->aiplayer_2->alive)
        {
            ui->battleyou_2->setEnabled(true);
        }
        if(ui->aiplayer_3->alive)
        {
            ui->battleyou_3->setEnabled(true);
        }
        if(ui->aiplayer_4->alive)
        {
            ui->battleyou_4->setEnabled(true);
        }
        if(ui->aiplayer_5->alive)
        {
            ui->battleyou_5->setEnabled(true);
        }
        ui->mypass->setEnabled(true);
    }
    ui->mygiveup->setDisabled(true);
    ui->mylook->setDisabled(true);
    ui->myonce->setDisabled(true);
    ui->mytreble->setDisabled(true);
    ui->mytwice->setDisabled(true);
    ui->my_show->setText(QString::number(mycoin));
}
void Widget::mygived()
{
    ismyalive=false;
    ui->mygiveup->setDisabled(true);
    ui->mylook->setDisabled(true);
    ui->myonce->setDisabled(true);
    ui->mytreble->setDisabled(true);
    ui->mytwice->setDisabled(true);
    ui->mypass->setDisabled(true);
   turn1();;
}
void Widget::myshowhanded()
{
    mycoin=0;
    ui->my_show->setText(QString::number(mycoin));
    //
}
void Widget::paintEvent(QPaintEvent*)
{
    if(ismylooked)
    {
    QPainter painter1(this);
    QRect rect1 = ui->frame1->frameRect();
    QImage image1(":/images/"+QString::number(N->pocker[0])+".jpg");
    rect1.translate(ui->frame1->pos());
    painter1.drawImage(rect1,image1);
    QPainter painter2(this);
    QRect rect2 = ui->frame2->frameRect();
    QImage image2(":/images/"+QString::number(N->pocker[6])+".jpg");
    rect2.translate(ui->frame2->pos());
    painter2.drawImage(rect2,image2);
    QPainter painter3(this);
    QRect rect3 = ui->frame3->frameRect();
    QImage image3(":/images/"+QString::number(N->pocker[12])+".jpg");
    rect3.translate(ui->frame3->pos());
    painter3.drawImage(rect3,image3);
    }
}
bool Widget::mybating(PlayerForm*a)
{
    if(a->type!=type)
    {
        return type>a->type;
    }
    else
    {
        if(capital[0]!=a->capital[0])
        {
            return capital[0]>a->capital[0];
        }
        else
        {
            if(sub!=a->sub)
            {
                return sub>a->sub;
            }
            else
            {
                if(subsub!=a->subsub)
                {
                    return subsub>a->subsub;
                }
                else
                    return capital[1]<a->capital[1];
            }
        }
    }
}
void Widget::beginbattle1()
{
        if(mybating(ui->aiplayer_1))
        {
            ui->aiplayer_1->alive=false;
            ui->aiplayer_1->defeated();
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家击败了1号玩家！");
            ui->gamemessages->append(QString::number(type));
            ui->gamemessages->append(QString::number(ui->aiplayer_1->type));
            turn1();
        }
        else
        {
            ismyalive=false;
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家被1号玩家击败了！");
            ui->gamemessages->append(QString::number(type));
            ui->gamemessages->append(QString::number(ui->aiplayer_1->type));
        }
    }
 void Widget::beginbattle2()
    {
        if(mybating(ui->aiplayer_2))
        {
            ui->aiplayer_2->alive=false;
            ui->aiplayer_2->defeated();
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家击败了2号玩家！");

            turn1();
        }
        else
        {
            ismyalive=false;
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家被2号玩家击败了！");

        }
    }
void Widget::beginbattle3()
    {
        if(mybating(ui->aiplayer_3))
        {
            ui->aiplayer_3->alive=false;
            ui->aiplayer_3->defeated();
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家击败了3号玩家！");

            turn1();
        }
        else
        {
            ismyalive=false;
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家被3号玩家击败了！");

        }
    }
void Widget::beginbattle4()
    {
        if(mybating(ui->aiplayer_4))
        {
            ui->aiplayer_4->alive=false;
            ui->aiplayer_4->defeated();
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家击败了4号玩家！");

            turn1();
        }
        else
        {
            ismyalive=false;
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家被4号玩家击败了！");
        }
    }
void Widget::beginbattle5()
    {
        if(mybating(ui->aiplayer_5))
        {
            ui->aiplayer_5->alive=false;
            ui->aiplayer_5->defeated();
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家击败了5号玩家！");
            turn1();
        }
        else
        {
            ismyalive=false;
            ui->mygiveup->setDisabled(true);
            ui->mylook->setDisabled(true);
            ui->myonce->setDisabled(true);
            ui->mytreble->setDisabled(true);
            ui->mytwice->setDisabled(true);
            ui->gamemessages->append("0号玩家被5号玩家击败了！");
        }
    }
bool Widget::bating(PlayerForm*b,PlayerForm*a)
{
    if(a->type!=b->type)
    {
        return b->type>a->type;
    }
    else
    {
        if(b->capital[0]!=a->capital[0])
        {
            return b->capital[0]>a->capital[0];
        }
        else
        {
            if(b->sub!=a->sub)
            {
                return b->sub>a->sub;
            }
            else
            {
                if(b->subsub!=a->subsub)
                {
                    return b->subsub>a->subsub;
                }
                else
                    return b->capital[1]<a->capital[1];
            }
        }
    }
}
void Widget::myjudge()
{
    if(onescard[1]%13>onescard[2]%13)
    {
        int temp = onescard[2];
        onescard[2]=onescard[1];
        onescard[1]=temp;
    }
    if(onescard[0]%13>onescard[2]%13)
    {
        int temp = onescard[2];
        onescard[2]=onescard[0];
        onescard[0]=temp;
    }
    if(onescard[0]%13>onescard[1]%13)
    {
        int temp = onescard[0];
        onescard[0]=onescard[1];
        onescard[1]=temp;
    }
if(onescard[0]+2==onescard[2]&&onescard[1]+1==onescard[2])
{
    type=5;
    capital[0]=onescard[2]%13;
    capital[1]=onescard[2]/13;
}
else if(onescard[0]%13==0&&onescard[1]==onescard[0]+11&&onescard[2]==onescard[0]+12)
{
    type=5;
    capital[0]=13;
    capital[1]=onescard[0]/13;
}
else if(onescard[0]%13==onescard[1]%13&&onescard[1]%13==onescard[2]%13)
{
    type=4;
    if(onescard[0]%13==0)
    {
        capital[0]=13;
    }
    else
    {
        capital[0]=onescard[0]%13;
    }
}
else if((onescard[0]%13+2==onescard[2]%13)&&(onescard[1]%13+1==onescard[2]%13))
{
    type=3;
    capital[0]=onescard[2]%13;
    capital[1]=onescard[2]/13;
}
else if(onescard[0]%13==0&&onescard[1]%13==11&&onescard[2]%13==12)
{
    type=3;
    capital[0]=13;
    capital[1]=onescard[0]/13;
}
else if(onescard[0]/13==onescard[1]/13&&onescard[1]/13==onescard[2]/13)
{
    type=2;
    if(onescard[0]%13==0)
    {
          capital[0]=13;
          sub=onescard[2]%13;
          subsub=onescard[1]%13;
    }
    else
    {
          capital[0]=onescard[2]%13;
          sub=onescard[1]%13;
          subsub=onescard[0]%13;
    }
    capital[1]=onescard[0]/13;
}
else if(onescard[0]%13==onescard[1]%13||onescard[0]%13==onescard[2]%13||onescard[1]%13==onescard[2]%13)
{
    type=1;
    if(onescard[0]%13!=onescard[2]%13)
    {
        if(onescard[1]%13==onescard[2]%13)
        {
            if(onescard[1]%13==0)
            {
                capital[0]=13;
                sub=onescard[0]%13;
            }
            else
            {
                capital[0]=onescard[1]%13;
                sub=onescard[0]%13;
            }
            capital[1]=onescard[0]/13;
        }
        else
        {
            if(onescard[1]%13==0)
            {
                capital[0]=13;
                sub=onescard[2]%13;
            }
            else
            {
                capital[0]=onescard[1]%13;
                sub=onescard[2]%13;
            }
            capital[1]=onescard[2]%13;
        }
    }
    else
    {
        if(onescard[0]%13==0)
        {
            capital[0]=13;
            sub=onescard[1]%13;
        }
        else
        {
            capital[0]=onescard[1]%13;
            sub=onescard[1]%13;
        }
        capital[1]=onescard[2]/13;
    }

}
else
{
    type=0;
    if(onescard[0]%13==0)
    {
        capital[0]=13;
        sub=onescard[2]%13;
        subsub=onescard[1]%13;
        capital[1]=onescard[0]/13;
    }
    else
    {
        capital[0]=onescard[2]%13;
        sub=onescard[1]%13;
        subsub=onescard[0]%13;
        capital[1]=onescard[2]%13;
    }
}
}

void Widget::turn1()
{
    ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家回合开始");
    qsrand(QTime::currentTime().msec());
    int dic=qrand()%100;
    if(ui->aiplayer_1->confi>(dic+40))
    {
        if(!ui->aiplayer_1->looked)
        {
        if(ui->aiplayer_1->coin>4*current)
        {
        ui->aiplayer_1->coin-=4*current;
        current*=4;
        total+=current;
        ui->total_show->setText(QString(total));
        ui->aiplayer_1->setcoin();
        ui->current_show->setText(QString::number(current));
        ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家盲押四倍");
        turnover(1);
        //结束回合
        }
        else
        {
         total+=ui->aiplayer_1->coin;
         ui->aiplayer_1->coin=0;
         ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
         if(ui->aiplayer_3->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_3))
             {
                 ui->aiplayer_3->defeated();
                 ui->aiplayer_3->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_2->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_2))
             {
                 ui->aiplayer_2->defeated();
                 ui->aiplayer_2->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_4->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_4))
             {
                 ui->aiplayer_4->defeated();
                 ui->aiplayer_4->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_5->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_5))
             {
                 ui->aiplayer_5->defeated();
                 ui->aiplayer_5->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ismyalive)
         {
             if(battled(ui->aiplayer_1))
             {
                 ismyalive=false;
                 ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
        }//梭哈结算；
        }
        else
        {
            if(ui->aiplayer_1->coin>4*current)
            {
            ui->aiplayer_1->coin-=4*current;
            current*=2;
            total+=current;
            ui->total_show->setText(QString(total));
            ui->aiplayer_1->setcoin();
            ui->current_show->setText(QString::number(current));
            ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"押了4倍，全场翻2倍");
            turnover(1);
            }
            //回合结束
            else
            {
             total+=ui->aiplayer_1->coin;
             ui->aiplayer_1->coin=0;
             ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
             if(ui->aiplayer_3->alive)
             {
                 if(bating(ui->aiplayer_1,ui->aiplayer_3))
                 {
                     ui->aiplayer_3->defeated();
                     ui->aiplayer_3->alive=false;
                     ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                    turnover(1);
                 }
                 else
                     gameover(1);
             }
             else if(ui->aiplayer_2->alive)
             {
                 if(bating(ui->aiplayer_1,ui->aiplayer_2))
                 {
                     ui->aiplayer_2->defeated();
                     ui->aiplayer_2->alive=false;
                     ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                    turnover(1);
                 }
                 else
                     gameover(1);
             }
             else if(ui->aiplayer_4->alive)
             {
                 if(bating(ui->aiplayer_1,ui->aiplayer_4))
                 {
                     ui->aiplayer_4->defeated();
                     ui->aiplayer_4->alive=false;
                     ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                    turnover(1);
                 }
                 else
                     gameover(1);
             }
             else if(ui->aiplayer_5->alive)
             {
                 if(bating(ui->aiplayer_1,ui->aiplayer_5))
                 {
                     ui->aiplayer_5->defeated();
                     ui->aiplayer_5->alive=false;
                     ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                    turnover(1);
                 }
                 else
                     gameover(1);
             }
             else if(ismyalive)
             {
                 if(battled(ui->aiplayer_1))
                 {
                     ismyalive=false;
                     ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                    turnover(1);
                 }
                 else
                     gameover(1);
             }
             }
        }
    }
    else if(ui->aiplayer_1->confi>(dic+10))
    {
        if(!ui->aiplayer_1->looked)
        {
        qsrand(QTime::currentTime().msec());
        int ldic=qrand()%100;
        if(ldic>=50)
        {
        if(ui->aiplayer_1->coin>2*current)
        {
        ui->aiplayer_1->coin-=2*current;
        current*=2;
        total+=current;
        ui->total_show->setText(QString(total));
        ui->aiplayer_1->setcoin();
        ui->current_show->setText(QString::number(current));
        ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家盲押两倍，全场翻两倍");
        turnover(1);
        }
        else
        {
         total+=ui->aiplayer_1->coin;
         ui->aiplayer_1->coin=0;
         ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
         if(ui->aiplayer_3->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_3))
             {
                 ui->aiplayer_3->defeated();
                 ui->aiplayer_3->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_2->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_2))
             {
                 ui->aiplayer_2->defeated();
                 ui->aiplayer_2->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_4->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_4))
             {
                 ui->aiplayer_4->defeated();
                 ui->aiplayer_4->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ui->aiplayer_5->alive)
         {
             if(bating(ui->aiplayer_1,ui->aiplayer_5))
             {
                 ui->aiplayer_5->defeated();
                 ui->aiplayer_5->alive=false;
                 ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
         else if(ismyalive)
         {
             if(battled(ui->aiplayer_1))
             {
                 ismyalive=false;
                 ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                 turnover(1);
             }
             else
                 gameover(1);
         }
        }
        }
        else
        {
         ui->aiplayer_1->looked=true;
         ui->aiplayer_1->setlooked();
         ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择看一眼自己的牌");
         if(type==0)
         {
             qsrand(QTime::currentTime().msec());
             int rdic=qrand()%100;
             if(rdic>60)
             {
                 if(ui->aiplayer_1->coin<current*2)
                 {
                  total+=ui->aiplayer_1->coin;
                  ui->aiplayer_1->coin=0;
                  ui->total_show->setText(QString::number(total));
                  ui->aiplayer_1->setcoin();
                  ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
                  if(ui->aiplayer_3->alive)
                  {
                      if(bating(ui->aiplayer_1,ui->aiplayer_3))
                      {
                          ui->aiplayer_3->defeated();
                          ui->aiplayer_3->alive=false;
                          ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                          turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          gameover(1);
                      }
                  }
                  else if(ui->aiplayer_2->alive)
                  {
                      if(bating(ui->aiplayer_1,ui->aiplayer_2))
                      {
                          ui->aiplayer_2->defeated();
                          ui->aiplayer_2->alive=false;
                           ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                          turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          gameover(1);
                      }
                  }
                  else if(ui->aiplayer_4->alive)
                  {
                      if(bating(ui->aiplayer_1,ui->aiplayer_4))
                      {
                          ui->aiplayer_4->defeated();
                          ui->aiplayer_4->alive=false;
                           ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                          turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          gameover(1);
                      }
                  }
                  else if(ui->aiplayer_5->alive)
                  {
                      if(bating(ui->aiplayer_1,ui->aiplayer_5))
                      {
                          ui->aiplayer_5->defeated();
                          ui->aiplayer_5->alive=false;
                           ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                          turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          gameover(1);
                      }
                  }
                  else if(ismyalive)
                  {
                      if(battled(ui->aiplayer_1))
                      {
                          ismyalive=false;
                           ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                           turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          gameover(1);
                      }
                  }
                 }
                 else
                 {
                     total+=2*current;
                     ui->total_show->setText(QString::number(total));
                     ui->aiplayer_1->coin-=current*2;
                     ui->aiplayer_1->setcoin();
                     ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择押两倍");
                     turnover(1);
                 }
             }
             else
             {
                 ui->aiplayer_1->alive=false;
                 ui->aiplayer_1->defeated();
                 ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择这局先撤了，“留的青山在，不怕没柴烧”");
                 turnover(1);
             }
         }
         else if(type==1)
         {
             if(ui->aiplayer_1->coin>2*current)
             {
              ui->aiplayer_1->confi+=30;
              total+=current*2;
              ui->aiplayer_1->coin-=2*current;
              ui->total_show->setText(QString::number(total));
              ui->aiplayer_1->setcoin();
              ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择押两倍");
              turnover(1);
             }
             else
             {
                 ui->aiplayer_1->alive=false;
                 ui->aiplayer_1->defeated();
                 ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择这局先撤了，“留的青山在，不怕没柴烧”");
                 turnover(1);
             }
         }
         else if(type<=3)
         {
             ui->aiplayer_1->confi+=40;
             if(ui->aiplayer_1->coin>4*current)
             {
              total+=current*4;
              ui->aiplayer_1->coin-=current*4;
              ui->total_show->setText(QString::number(total));
              ui->aiplayer_1->setcoin();
              ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家选择押四倍！");
              turnover(1);
             }
             else
             {
                 total+=ui->aiplayer_1->coin;
                 ui->aiplayer_1->coin=0;
                 ui->total_show->setText(QString::number(total));
                 ui->aiplayer_1->setcoin();
                 ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
                 if(ui->aiplayer_3->alive)
                 {
                     if(bating(ui->aiplayer_1,ui->aiplayer_3))
                     {
                         ui->aiplayer_3->defeated();
                         ui->aiplayer_3->alive=false;
                         ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                        turnover(1);
                     }
                     else
                         gameover(1);
                 }
                 else if(ui->aiplayer_2->alive)
                 {
                     if(bating(ui->aiplayer_1,ui->aiplayer_2))
                     {
                         ui->aiplayer_2->defeated();
                         ui->aiplayer_2->alive=false;
                         ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                        turnover(1);
                     }
                     else
                         gameover(1);
                 }
                 else if(ui->aiplayer_4->alive)
                 {
                     if(bating(ui->aiplayer_1,ui->aiplayer_4))
                     {
                         ui->aiplayer_4->defeated();
                         ui->aiplayer_4->alive=false;
                         ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                        turnover(1);
                     }
                     else
                         gameover(1);
                 }
                 else if(ui->aiplayer_5->alive)
                 {
                     if(bating(ui->aiplayer_1,ui->aiplayer_5))
                     {
                         ui->aiplayer_5->defeated();
                         ui->aiplayer_5->alive=false;
                         ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                        turnover(1);
                     }
                     else
                         gameover(1);
                 }
                 else if(ismyalive)
                 {
                     if(battled(ui->aiplayer_1))
                     {
                         ismyalive=false;
                         ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                         turnover(1);
                     }
                     else
                         gameover(1);
                 }
             }
         }
         else
         {
              ui->aiplayer_1->confi+=60;
              if(ui->aiplayer_1->coin<current*4)
              {
              total+=ui->aiplayer_1->coin;
              ui->aiplayer_1->coin=0;
              ui->total_show->setText(QString::number(total));
              ui->aiplayer_1->setcoin();
              ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"号玩家梭哈，决战时刻来临力！");
              if(ui->aiplayer_3->alive)
              {
                  if(bating(ui->aiplayer_1,ui->aiplayer_3))
                  {
                      ui->aiplayer_3->defeated();
                      ui->aiplayer_3->alive=false;
                      ui->gamemessages->append(QString::number(ui->aiplayer_3->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                     turnover(1);
                  }
                  else
                      gameover(1);
              }
              else if(ui->aiplayer_2->alive)
              {
                  if(bating(ui->aiplayer_1,ui->aiplayer_2))
                  {
                      ui->aiplayer_2->defeated();
                      ui->aiplayer_2->alive=false;
                      ui->gamemessages->append(QString::number(ui->aiplayer_2->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                     turnover(1);
                  }
                  else
                      gameover(1);
              }
              else if(ui->aiplayer_4->alive)
              {
                  if(bating(ui->aiplayer_1,ui->aiplayer_4))
                  {
                      ui->aiplayer_4->defeated();
                      ui->aiplayer_4->alive=false;
                      ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                     turnover(1);
                  }
                  else
                      gameover(1);
              }
              else if(ui->aiplayer_5->alive)
              {
                  if(bating(ui->aiplayer_1,ui->aiplayer_5))
                  {
                      ui->aiplayer_5->defeated();
                      ui->aiplayer_5->alive=false;
                      ui->gamemessages->append(QString::number(ui->aiplayer_5->number)+"号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                     turnover(1);
                  }
                  else
                      gameover(1);
              }
              else if(ismyalive)
              {
                  if(battled(ui->aiplayer_1))
                  {
                      ismyalive=false;
                      ui->gamemessages->append("0号玩家被"+QString::number(ui->aiplayer_1->number)+"号玩家击败了");
                     turnover(1);
                  }
                  else
                      gameover(1);
              }
              }
              else
              {
                  total+=4*current;
                  ui->aiplayer_1->coin-=4*current;
                  current*=2;
                  ui->total_show->setText(QString::number(total));
                  ui->aiplayer_1->setcoin();
                  ui->gamemessages->append(QString::number(ui->aiplayer_4->number)+"号玩家选择押四倍，全场翻两倍");
                  turnover(1);
              }
         }
        }
    }
        else
        {
            qsrand(QTime::currentTime().msec());
            int rdic=qrand()%100;
            if(rdic>40)
            {
                ui->aiplayer_1->defeated();
                ui->aiplayer_1->alive=false;
                ui->gamemessages->append(QString::number(ui->aiplayer_1->number)+"选择先溜，“留的青山在，不怕没柴烧。”");
                turnover(1);
            }
            else
            {
                if(2*current<ui->aiplayer_1->coin)
                {
                ui->aiplayer_1->coin-=2*current;
                total+=2*current;
                ui->total_show->setText(QString(total));

                turnover(1);
                }
                else
                {
                    if(ui->aiplayer_3->alive)
                    {
                        if(bating(ui->aiplayer_1,ui->aiplayer_3))
                        {
                            ui->aiplayer_3->defeated();
                            ui->aiplayer_3->alive=false;
                        }
                        else
                            gameover(1);
                    }
                    else if(ui->aiplayer_2->alive)
                    {
                        if(bating(ui->aiplayer_1,ui->aiplayer_2))
                        {
                            ui->aiplayer_2->defeated();
                            ui->aiplayer_2->alive=false;
                        }
                        else
                            gameover(1);
                    }
                    else if(ui->aiplayer_4->alive)
                    {
                        if(bating(ui->aiplayer_1,ui->aiplayer_4))
                        {
                            ui->aiplayer_4->defeated();
                            ui->aiplayer_4->alive=false;
                        }
                        else
                            gameover(1);
                    }
                    else if(ui->aiplayer_5->alive)
                    {
                        if(bating(ui->aiplayer_1,ui->aiplayer_5))
                        {
                            ui->aiplayer_5->defeated();
                            ui->aiplayer_5->alive=false;
                        }
                        else
                            gameover(1);
                    }
                    else if(ismyalive)
                    {
                        if(battled(ui->aiplayer_1))
                        {
                            ismyalive=false;
                        }
                        else
                            gameover(1);
                    }

                }
            }
         }
    }
    else
         {
             qsrand(QTime::currentTime().msec());
             int ldic=qrand()%100;
             if(ldic>30&&ui->aiplayer_1->coin<=current)
             {
                  ui->aiplayer_1->looked=true;
                  ui->aiplayer_1->setlooked();
                  if(type==0)
                  {
                      qsrand(QTime::currentTime().msec());
                      int rdic=qrand()%100;
                      if(rdic>60)
                      {
                          if(ui->aiplayer_1->coin<current*2)
                          {
                           total+=ui->aiplayer_1->coin;
                           ui->aiplayer_1->coin=0;
                           ui->total_show->setText(QString::number(total));
                           ui->aiplayer_1->setcoin();
                           if(ui->aiplayer_3->alive)
                           {
                               if(bating(ui->aiplayer_1,ui->aiplayer_3))
                               {
                                   ui->aiplayer_3->defeated();
                                   ui->aiplayer_3->alive=false;
                               }
                               else
                               {
                                   ui->aiplayer_1->alive=false;
                                   turnover(1);
                               }
                           }
                           else if(ui->aiplayer_2->alive)
                           {
                               if(bating(ui->aiplayer_1,ui->aiplayer_2))
                               {
                                   ui->aiplayer_2->defeated();
                                   ui->aiplayer_2->alive=false;
                               }
                               else
                               {
                                   ui->aiplayer_1->alive=false;
                                   turnover(1);
                               }
                           }
                           else if(ui->aiplayer_4->alive)
                           {
                               if(bating(ui->aiplayer_1,ui->aiplayer_4))
                               {
                                   ui->aiplayer_4->defeated();
                                   ui->aiplayer_4->alive=false;
                               }
                               else
                               {
                                   ui->aiplayer_1->alive=false;
                                   turnover(1);
                               }
                           }
                           else if(ui->aiplayer_5->alive)
                           {
                               if(bating(ui->aiplayer_1,ui->aiplayer_5))
                               {
                                   ui->aiplayer_5->defeated();
                                   ui->aiplayer_5->alive=false;
                               }
                               else
                               {
                                   ui->aiplayer_1->alive=false;
                                   turnover(1);
                               }
                           }
                           else if(ismyalive)
                           {
                               if(battled(ui->aiplayer_1))
                               {
                                   ismyalive=false;
                               }
                               else
                               {
                                   ui->aiplayer_1->alive=false;
                                   turnover(1);
                               }
                           }
                          }
                          else
                          {
                              total+=2*current;
                              ui->total_show->setText(QString::number(total));
                              ui->aiplayer_1->coin-=current*2;
                              ui->aiplayer_1->setcoin();
                              turnover(1);
                          }
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          ui->aiplayer_1->defeated();
                          turnover(1);
                      }
                  }
                  else if(type==1)
                  {
                      if(ui->aiplayer_1->coin>2*current)
                      {
                       ui->aiplayer_1->confi+=30;
                       total+=current*2;
                       ui->aiplayer_1->coin-=2*current;
                       ui->total_show->setText(QString::number(total));
                       ui->aiplayer_1->setcoin();
                       turnover(1);
                      }
                      else
                      {
                          ui->aiplayer_1->alive=false;
                          ui->aiplayer_1->defeated();
                          turnover(1);
                      }
                  }
                  else if(type<=3)
                  {
                      ui->aiplayer_1->confi+=30;
                      if(ui->aiplayer_1->coin>4*current)
                      {
                       total+=current*4;
                       ui->aiplayer_1->coin-=current*4;
                       ui->total_show->setText(QString::number(total));
                       ui->aiplayer_1->setcoin();
                       turnover(1);
                      }
                      else
                      {
                          total+=ui->aiplayer_1->coin;
                          ui->aiplayer_1->coin=0;
                          ui->total_show->setText(QString::number(total));
                          ui->aiplayer_1->setcoin();
                          if(ui->aiplayer_3->alive)
                          {
                              if(bating(ui->aiplayer_1,ui->aiplayer_3))
                              {
                                  ui->aiplayer_3->defeated();
                                  ui->aiplayer_3->alive=false;
                              }
                              else
                                  gameover(1);
                          }
                          else if(ui->aiplayer_2->alive)
                          {
                              if(bating(ui->aiplayer_1,ui->aiplayer_2))
                              {
                                  ui->aiplayer_2->defeated();
                                  ui->aiplayer_2->alive=false;
                              }
                              else
                                  gameover(1);
                          }
                          else if(ui->aiplayer_4->alive)
                          {
                              if(bating(ui->aiplayer_1,ui->aiplayer_4))
                              {
                                  ui->aiplayer_4->defeated();
                                  ui->aiplayer_4->alive=false;
                              }
                              else
                                  gameover(1);
                          }
                          else if(ui->aiplayer_5->alive)
                          {
                              if(bating(ui->aiplayer_1,ui->aiplayer_5))
                              {
                                  ui->aiplayer_5->defeated();
                                  ui->aiplayer_5->alive=false;
                              }
                              else
                                  gameover(1);
                          }
                          else if(ismyalive)
                          {
                              if(battled(ui->aiplayer_1))
                              {
                                  ismyalive=false;
                              }
                              else
                                  gameover(1);
                          }
                      }
                  }
                  else
                  {
                       ui->aiplayer_1->confi+=45;
                       if(ui->aiplayer_1->coin<current*4)
                       {
                       total+=ui->aiplayer_1->coin;
                       ui->aiplayer_1->coin=0;
                       ui->total_show->setText(QString::number(total));
                       ui->aiplayer_1->setcoin();
                       if(ui->aiplayer_3->alive)
                       {
                           if(bating(ui->aiplayer_1,ui->aiplayer_3))
                           {
                               ui->aiplayer_3->defeated();
                               ui->aiplayer_3->alive=false;
                           }
                           else
                               gameover(1);
                       }
                       else if(ui->aiplayer_2->alive)
                       {
                           if(bating(ui->aiplayer_1,ui->aiplayer_2))
                           {
                               ui->aiplayer_2->defeated();
                               ui->aiplayer_2->alive=false;
                           }
                           else
                               gameover(1);
                       }
                       else if(ui->aiplayer_4->alive)
                       {
                           if(bating(ui->aiplayer_1,ui->aiplayer_4))
                           {
                               ui->aiplayer_4->defeated();
                               ui->aiplayer_4->alive=false;
                           }
                           else
                               gameover(1);
                       }
                       else if(ui->aiplayer_5->alive)
                       {
                           if(bating(ui->aiplayer_1,ui->aiplayer_5))
                           {
                               ui->aiplayer_5->defeated();
                               ui->aiplayer_5->alive=false;
                           }
                           else
                               gameover(1);
                       }
                       else if(ismyalive)
                       {
                           if(battled(ui->aiplayer_1))
                           {
                               ismyalive=false;
                           }
                           else
                               gameover(1);
                       }
                       }
                       else
                       {
                           total+=4*current;
                           ui->aiplayer_1->coin-=4*current;
                           current*=2;
                           ui->total_show->setText(QString::number(total));
                           ui->aiplayer_1->setcoin();
                           turnover(1);
                       }
                  }
             }
             else
             {
                 ui->aiplayer_1->coin-=current;
                 total+=current;
                 ui->aiplayer_1->setcoin();
                 ui->total_show->setText(QString::number(total));
             }
         }
}
void Widget::gameover(int)
{

}
bool Widget::battled(PlayerForm*)
{
    return 1;
}
void Widget::turnover(int)
{
    ;
}
Widget::~Widget()
{
    delete N;
    delete ui;
}

